<?php
/**
 * Created by PhpStorm.
 * User: huawei
 * Date: 2021/11/4
 * Time: 11:09
 */

namespace yuexiu;

use think\facade\Cache;
class Wx
{
    public $domain1 = "https://fangbao-api.yuexiuproperty.cn";
    public $appId ="ec2493f7";
    public $appSecret ="1234567890";



    public function res($rs){
        $result = json_decode($rs,true);

        if($result&&$result['code']==200){
            return $result["data"];
        }else{
            errMsg(isset($result["msg"])?$result["msg"]:"接口出错");
        }
    }
    public function  makePreString($params)
    {
        $params['sign_key'] = $this->appSecret;
        $params = filterParams($params);
        // 按参数键名排序参数
        ksort($params);
        // 参数连接⽅式：age=12&name=张三&sex=male
        $preString = urldecode(http_build_query($params));
        return $preString;
    }
    public function getsign($data){
        $params = $data;
        $params["merchantId"] = $this->appId;
        $params["sign_key"] = $this->appSecret;
        $timestamp = time();
        $params["timestamp"] = (string)$timestamp;

        $md5string = $this->makePreString($params);
        $sign = md5($md5string);
        $data["merchantId"] = $this->appId;
        $data["sign"] = $sign;
        $data["timestamp"] = $timestamp;
        return $data;
    }
    /**
     * @title curl请求
     * @param string $url 请求的url
     * @param string $method 请求的方式 GET/POST/PUT/DELETE
     * @param string $data 发送的数据
     * @param array $header 请求头
     * @return bool|false|string 返回
     */
    public function do_curl($url, $method ='GET', $data = '',array $header=[]){
        if ($method == 'POST') {
            //使用crul模拟
            $ch = curl_init();
            //禁用https
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

            //允许请求以文件流的形式返回
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_POST, 1);
            if(empty($header)){
                curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type: application/json; charset=utf-8"));
            }else{
                curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
            }

            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_URL, $url);
            $result = curl_exec($ch); //执行发送


            curl_close($ch);
        }else {
            if (ini_get('allow_fopen_url') == '1') {
                $result = file_get_contents($url);
            }else {
                //使用crul模拟
                $ch = curl_init();
                //允许请求以文件流的形式返回
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

                //禁用https
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

                curl_setopt($ch, CURLOPT_URL, $url);
                $result = curl_exec($ch); //执行发送
                curl_close($ch);
            }
        }
        return $result;
    }
    //获取微信公从号access_token，有效期7200 s
    public function get_token() {
        $url = $this->domain1."/im-new/wx/mp/getAccessToken";
        $data=[
            "wxAppId"=>"wx5d7acdf9a00bb6e8"
        ];
        var_dump(json_encode($this->getsign($data)));
        $rs = httpPost($url,json_encode($this->getsign($data)));
        var_dump($rs);
        $data = $this->res($rs);
        $accessToken=$data["accessToken"];
        return $accessToken;
    }
    //获取jsapi_ticket，有效期7200 s
    public function getwxappurl($area) {
        $arr=[
            "ASuu0myA"=>"/pages/common/activity/index?landingPageId=1444",
            "11hMO8ja"=>"/pages/common/activity/index?landingPageId=1449",
            "KIiBvEqV"=>"/pages/common/activity/index?landingPageId=1450",
            "Vb9rv47X"=>"/pages/common/activity/index?landingPageId=1443",
            "kIJkusb8"=>"/pages/common/activity/index?landingPageId=1445",
            "guN7mouM"=>"/pages/common/activity/index?landingPageId=1446",
            "1aHTPZOK"=>"/pages/common/activity/index?landingPageId=1447",
            "TsnYW0Kb"=>"/pages/common/activity/index?landingPageId=1448 ",
            "IrOaUOpG"=>"/pages/common/activity/index?landingPageId=1460"
        ];
        return isset($arr[$area])?$arr[$area]:"/pages/index/index";
    }

    public function sendmsg($openid,$keyword3,$area,$nickname){
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->get_token();
        if(!Cache::has("wxmsg_".$openid)){
            $data = [
                "touser"=>$openid,
                "template_id"=>"SgqVDZk29tHmzGQrHeF4adVgXaNrR1ffTKkuckiXSUM",
                "url"=>"",
                "miniprogram"=>[
                    "appid"=>"wx82dc809f8b63c37f",
                    "pagepath"=>$this->getwxappurl($area)
                ],
                "data"=>[
                    "first"=>[
                        "value"=>"尊敬的越秀房宝客户,您的游戏次数已增加",
                        "color"=>"#173177"
                    ],
                    "keyword1"=>[
                        "value"=>$nickname,
                        "color"=>"#173177"
                    ],
                    "keyword2"=>[
                        "value"=>"严选好玩，越秀酷跑领红包",
                        "color"=>"#173177"
                    ],
                    "keyword3"=>[
                        "value"=>"$keyword3",
                        "color"=>"#173177"
                    ],
                    "remark"=>[
                        "value"=>"点击开始酷跑，领取越秀红包",
                        "color"=>"#173177"
                    ],
                ]
            ];

            $res =json_decode($this->do_curl($url,"POST",json_encode($data,JSON_UNESCAPED_UNICODE)),true);

            Cache::set("wxmsg_".$openid,1,300);
        };
//        first
//keyword1
//keyword2
//keyword3
//remark
//
    }
}